package itpenguin.plugin;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;

import org.eclipse.jface.text.templates.TemplateContext;
import org.eclipse.jface.text.templates.TemplateVariableResolver;


public class GenerateRundomId extends TemplateVariableResolver {

	@Override
	protected String resolve(TemplateContext context) {
		return byte2HexString((new Date()).toString());
	}
	
	private static String byte2HexString(String message)  {

		MessageDigest md = null;
		try {
			md = MessageDigest.getInstance("MD5");
		} catch (NoSuchAlgorithmException e) {
		}
		md.update(message.getBytes());
		StringBuffer buff = new StringBuffer();
		byte[] input = md.digest();
		int count = input.length;
		for(int i= 0; i< count; i++){
			buff.append(Integer.toHexString( (input[i]>> 4) & 0x0F ) );
			buff.append(Integer.toHexString( input[i] & 0x0F ) );
		}
		return buff.toString();
	}

}
